var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/businessComponent/imp/service.tsx
var service_exports = {};
__export(service_exports, {
  autoGenerateTask: () => autoGenerateTask,
  checkGenerateTask: () => checkGenerateTask,
  converSourceData: () => converSourceData,
  getFlowList: () => getFlowList,
  getSeletcedList: () => getSeletcedList,
  getTreeList: () => getTreeList,
  loadSourceConfig: () => loadSourceConfig,
  loadSourceData: () => loadSourceData,
  loadSourceTree: () => loadSourceTree
});
module.exports = __toCommonJS(service_exports);
var import_functionalComponent = require("../../functionalComponent");
var import_util = require("../../util");
var import_utils = require("./utils");
var bizTypeArray = ["reference_source_table_type", "generate_target_table_human_type"];
function omitNull(data) {
  if (!data || !import_util.iup.isObject(data)) {
    return data;
  }
  return Object.keys(data).reduce((p, k) => {
    if (!import_util.iup.isNullOrEmpty(data[k], "")) {
      p[k] = data[k];
    }
    return p;
  }, {});
}
async function getFlowList({ bizCode, orgId, tenantId, bizType, contractTypes }) {
  const [success, result] = await import_util.iup.request.bodyWithArray({
    url: "/iup-rule-engine/flow/definition/getFlowDefinitionChoseList",
    data: omitNull({ bizCode, orgId, tenantId, bizType: bizTypeArray[bizType], contractTypes })
  });
  if (success) {
    return result;
  }
  import_util.iup.alert(result);
  return void 0;
}
async function loadSourceTree({ bizCode, tableName, flowCode, sourceBizCode }) {
  const [success, result] = await import_util.iup.request.getWithArray({
    url: "/iup-rule-engine/helpInfo/getTreeHelpInfo",
    data: omitNull({ targetBizCode: bizCode, targetTable: tableName, flowCode, sourceBizCode })
  });
  if (success) {
    const sourceNodes = [];
    import_util.iup.loopChildren({ children: result }, (node) => {
      if (node.mappingFormId) {
        sourceNodes.push(node);
      } else {
        node.disabled = true;
      }
    });
    return [result, sourceNodes];
  }
  return [result, []];
}
async function loadSourceConfig({ sourceId }) {
  const [success, result] = await import_util.iup.request.getWithArray({
    url: "/iup-rule-engine/helpInfo/getHelpInfoById",
    data: omitNull({ id: sourceId })
  });
  !success && import_util.iup.alert(result);
  if (success) {
    const {
      ruleHelpInfoList = [],
      ruleHelpInfoQueryList = [],
      masterRuleHelpInfoList = [],
      masterRuleHelpInfoQueryList = [],
      popupName,
      targetTableType,
      allowMultiSelect,
      popupMode,
      masterFormId,
      treeFieldConfig,
      treeField
    } = result;
    return {
      ...result,
      popupMode,
      popupName,
      allowMultiSelect: targetTableType === 0 ? 0 : allowMultiSelect ?? 0,
      columns: (0, import_utils.columnsToTableColumns)(ruleHelpInfoList),
      queryItems: (0, import_utils.queryItemsToFormItems)(ruleHelpInfoQueryList),
      // 主表要用的
      masterColumns: (0, import_utils.columnsToTableColumns)(masterRuleHelpInfoList),
      masterQueryItems: (0, import_utils.queryItemsToFormItems)(masterRuleHelpInfoQueryList),
      masterFormId,
      treeFieldConfig,
      treeField
    };
  }
  return { columns: [], queryItems: [], popupName: "" };
}
async function loadSourceData({
  sourceId,
  pageIndex,
  pageSize,
  queryCondition,
  flowCode,
  busData,
  popupMode,
  masterId,
  treeFilterValue
}) {
  const urlSuffix = popupMode == 1 ? "getTreeHelpInfoList" : "getHelpInfoList";
  if (!sourceId) {
    return {};
  }
  const [success, result] = await import_util.iup.request.bodyWithArray({
    // getTreeHelpInfoList
    url: "/iup-rule-engine/helpInfo/" + urlSuffix,
    data: omitNull({
      id: sourceId,
      pageIndex,
      pageSize,
      queryCondition: queryCondition || {},
      flowCode,
      configMap: busData || {},
      masterId,
      treeFilterValue
    })
  });
  !success && import_util.iup.alert(result);
  return success ? result : { total: 0, list: [] };
}
async function converSourceData({ sourceId, flowCode, sourceDatas, detailTables, mainRuleHelpInfo }) {
  var _a;
  const data = {
    id: sourceId,
    flowCode,
    sourceDatas,
    mainRuleHelpInfo: ((_a = mainRuleHelpInfo == null ? void 0 : mainRuleHelpInfo.sourceDatas) == null ? void 0 : _a.length) ? mainRuleHelpInfo : null
  };
  const [success, result] = await import_util.iup.request.bodyWithArray({
    url: "/iup-rule-engine/helpInfo/mappingAimDatas",
    data: omitNull(detailTables ? { ...data, detailTables } : data)
  });
  !success && import_util.iup.alert(result);
  if (success && import_util.iup.isArray(result)) {
    return [
      success,
      result.reduce((p, d) => {
        const tmp = { ...p, [d.tableName]: d.data };
        if (d.modelName) {
          tmp[d.modelName] = d.data;
        }
        return tmp;
      }, {})
    ];
  }
  return [success, result];
}
async function getTreeList({ helpId, clientSqlFilter }) {
  const params = {
    node: "root"
  };
  if (clientSqlFilter) {
    if (import_util.iup.isObject(clientSqlFilter)) {
      params.clientJsonFilter = clientSqlFilter;
    } else {
      params.clientSqlFilter = import_util.iup.CryptoJS.encode(clientSqlFilter);
    }
  }
  if (import_util.iup.isFunction(helpId)) {
    helpId = await helpId();
  }
  if ((0, import_util.hasServiceAdapter)("commonHelp", "getTreeList")) {
    return await (0, import_util.execServiceAdapter)("commonHelp", "getTreeList", { ...params, helpId });
  }
  const {
    code,
    data,
    message: msg
  } = await import_util.iup.request.body({
    url: `baseservice/comhelp/getComHelpTreeList`,
    data: {
      ...params,
      helpId
    }
  });
  if (code === 0) {
    return data;
  }
  msg && import_functionalComponent.message.error(msg);
  return [];
}
async function getSeletcedList(params) {
  const [success, result] = await import_util.iup.request.bodyWithArray({
    url: "/iup-rule-engine/helpInfo/getSelectedList",
    data: omitNull(params)
  });
  !success && import_util.iup.alert(result);
  return success ? result : { total: 0, list: [] };
}
async function checkGenerateTask(params) {
  const { code, data } = await import_util.iup.request.get({
    url: "/iup-rule-engine/flow/definition/checkGenerateTask",
    data: omitNull(params)
  });
  code !== 0 && import_util.iup.alert(data);
  return code === 0 ? data : { generateTask: false, needUser: false };
}
async function autoGenerateTask(params) {
  const [success, result] = await import_util.iup.request.bodyWithArray({
    url: "/iup-rule-engine/flow/definition/generateTask",
    data: omitNull(params)
  });
  !success && import_util.iup.alert(result);
  return success;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  autoGenerateTask,
  checkGenerateTask,
  converSourceData,
  getFlowList,
  getSeletcedList,
  getTreeList,
  loadSourceConfig,
  loadSourceData,
  loadSourceTree
});
